package com.app.bimo.commonbusiness.util;

import android.telephony.PhoneNumberUtils;
import android.text.TextUtils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import androidx.annotation.NonNull;

public class DataUtil {
    private static String[] number_zh = new String[]{"零", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十"};//拾是多出位
    private static String[] unit_zh = new String[]{"", "十", "百", "千", "万", "亿"};


    public static String shuzizhuanzhongwen(Long number) {
//number_zh unit_zh
        StringBuffer sb = new StringBuffer();

        String str = number.toString();
        Stack<String> _stack = new Stack<String>();
        for (int i = 0; i < str.length(); i++) {
            _stack.push(number_zh[(int) (number % 10)]);// 带 拾、佰、仟...  修改为  (int) (number % 10) + getUnitZH(Long.valueOf(i))
            number = number / 10;
        }
        while (!_stack.isEmpty()) {
            sb.append(_stack.pop());
        }
        return sb.toString();
    }

    private static String getUnitZH(Long num) {
        if (num >= 5 && num < 8) {
            return getUnitZH(num - 4);
        } else if (num > 8) {
            return getUnitZH(num - 8);
        } else if (num == 8) {
            return unit_zh[5]; //亿
        } else if (num > 17) {
            return null;//暂不支持 亿亿
        } else {
            return unit_zh[num.intValue()];
        }
    }

    /**
     * 根据地址获得数据的字节流
     *
     * @param file 本地连接地址
     * @return
     */
    public static byte[] getImageFromLocalByUrl(File file) {
        try {
            InputStream inStream = new FileInputStream(file);
            byte[] btImg = readInputStream(inStream);// 得到图片的二进制数据
            return btImg;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 转换  第**章~第**章
     *
     * @param
     * @return
     */
    public static String groupChapter(int startIndex, int endIndex) {
        if (endIndex - startIndex > 1) {
            return "第" + startIndex + "章~" + "第" + endIndex + "章";

        } else {
            return "第" + startIndex + "章";
        }
    }

    /**
     * 从输入流中获取数据
     *
     * @param inStream 输入流
     * @return
     * @throws IOException
     * @throws Exception
     */
    private static byte[] readInputStream(InputStream inStream) throws IOException {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[10240];
        int len = 0;
        while ((len = inStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, len);
        }
        inStream.close();
        return outStream.toByteArray();

    }

    /**
     * 是否空字符串
     *
     * @param str
     * @return
     */
    public static boolean isEmpty(String str) {
        return PatternUtil.replaceBlank(str).length() == 0;
    }

    /**
            * 验证是否是URL
	 * @param url
	 * @return
             */
    public static boolean verifyUrl(String url){

        // URL验证规则
        String regEx ="[a-zA-z]+://[^\\s]*";
        // 编译正则表达式
        Pattern pattern = Pattern.compile(regEx);
        // 忽略大小写的写法
        // Pattern pat = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(url);
        // 字符串是否与正则表达式相匹配
        boolean rs = matcher.matches();
        return rs;

    }



    /**
     *  * 使用java正则表达式去掉多余的.与0
     *  * @param s
     *  * @return
     *  
     */
    public static String subZeroAndDot(String s) {
        if (s.indexOf(".") > 0) {
            s = s.replaceAll("0+?$", "");//去掉多余的0
            s = s.replaceAll("[.]$", "");//如最后一位是.则去掉
        }
        return s;
    }

    public static String getTime(long m) {

        if (m < 60) {//秒

            return "00  :  " + "00  :  " + NumFormat(0) + "  :  " + NumFormat(m);
        }
        if (m < 3600) {//分

            return "00  :  " + NumFormat(m / 60) + "  :  " + NumFormat(m % 60);
        }
        if (m < 3600 * 24) {//时

            return NumFormat(m / 60 / 60) + "  :  " + NumFormat(m / 60 % 60) + "  :  " + NumFormat(m % 60);
        }

        if (m >= 3600 * 24) {//天

            return NumFormat(m / 60 / 60) + "  :  " + NumFormat(m / 60 % 60) + "  :  " + NumFormat(m % 60);
        }
//        if (m >= 3600 * 24) {//天
//
//            return NumFormat(m / 60 / 60 / 24) + "天" + NumFormat(m / 60 / 60 % 24) + ":" + NumFormat(m / 60 % 60) + ":" + NumFormat(m % 60);
//        }

        return "00  :  00  :  00";
    }

    public static String NumFormat(long i) {
        if (String.valueOf(i).length() < 2) {
            return "0" + i;
        } else {
            return String.valueOf(i);
        }
    }


    /**
     * 获取月份
     *
     * @return
     */
    public static String getMonth(long time) {
        String month = TimeUnit.TimeStamp2Date(time + "", "M月");
        String currentMonth = TimeUnit.TimeStamp2Date((System.currentTimeMillis() / 1000) + "", "M月");
        if (month.equals(currentMonth)) {
            return "本月";
        } else {
            return month;
        }
    }

    /**
     * 向上取整每日签到人数  即1.1w人  会返回2w人
     *
     * @param num
     * @return
     */
    public static String getDaysignnum(int num) {
        if (num > 10000) {
            return Math.ceil(num / 10000) + "万人";
        } else {
            return num + "人";
        }

    }

    public static String StringToMd5(String psw) {
        {
            try {
                MessageDigest md5 = MessageDigest.getInstance("MD5");
                md5.update(psw.getBytes("UTF-8"));
                byte[] encryption = md5.digest();

                StringBuffer strBuf = new StringBuffer();
                for (int i = 0; i < encryption.length; i++) {
                    if (Integer.toHexString(0xff & encryption[i]).length() == 1) {
                        strBuf.append("0").append(Integer.toHexString(0xff & encryption[i]));
                    } else {
                        strBuf.append(Integer.toHexString(0xff & encryption[i]));
                    }
                }

                return strBuf.toString();
            } catch (NoSuchAlgorithmException e) {
                return "";
            } catch (UnsupportedEncodingException e) {
                return "";
            }
        }

    }


    /**
     * 判断手机号是否符合规范
     *
     * @param phoneNo 输入的手机号
     * @return
     */
    public static boolean isPhoneNumber(String phoneNo) {
        if (TextUtils.isEmpty(phoneNo)) {
            return false;
        }
        if (phoneNo.length() == 11) {
            for (int i = 0; i < 11; i++) {
                if (!PhoneNumberUtils.isISODigit(phoneNo.charAt(i))) {
                    return false;
                }
            }
            Pattern p = Pattern.compile("^((13[^4,\\D])" + "|(134[^9,\\D])" +
                    "|(14[5,7])" +
                    "|(15[^4,\\D])" +
                    "|(17[3,6-8])" +
                    "|(18[0-9]))\\d{8}$");
            Matcher m = p.matcher(phoneNo);
            return m.matches();
        }
        return false;
    }

    @NonNull
    public static String md5(String string) {
        if (TextUtils.isEmpty(string)) {
            return "";
        }
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
            byte[] bytes = md5.digest(string.getBytes());
            StringBuilder result = new StringBuilder();
            for (byte b : bytes) {
                String temp = Integer.toHexString(b & 0xff);
                if (temp.length() == 1) {
                    temp = "0" + temp;
                }
                result.append(temp);
            }
            return result.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * MD5(novelid + chapterids
     * + lastReadChapterid +
     * lastReadTime + readTime + 加密串)
     *
     * @param novelid
     * @return
     */
//    public static ArrayMap<String, String> getLeaveReadMap(String novelid) {
//
//        ArrayMap<String, String> map = new ArrayMap<>();
//        map.put("novelid", );
//        map.put("chapterids", );
//        map.put("lastReadChapterid", );
//        map.put("lastReadTime", );
//        map.put("readTime", );
//        map.put("sign", );
//        return map;
//
//    }
}
